package com.suixinsanren.design.compound.demo02;


import org.junit.Test;

public class CompoundTestDrive {

    @Test
    public void testObserver() {
        AbstractDuckFactory duckFactory = new CountingDuckFactory();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble mallardDuck = duckFactory.createMallardDuck();
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble rubberDuck = duckFactory.createRubberDuck();
        QuackAble gooseAdapter = new QuackCounter(new GooseAdapter(new Goose()));

        Flock flock = new Flock();
        flock.add(duckCall);
        flock.add(mallardDuck);
        flock.add(redHeadDuck);
        flock.add(rubberDuck);
        flock.add(gooseAdapter);

        QuackoLogist quackologist = new QuackoLogist();

        flock.addObserver(quackologist);

        testQuackAble(flock);

        System.out.println("鸭子叫了" + QuackCounter.getQuackCounter() + "次");
    }

    @Test
    public void testCombination() {
        AbstractDuckFactory duckFactory = new CountingDuckFactory();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble mallardDuck = duckFactory.createMallardDuck();
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble rubberDuck = duckFactory.createRubberDuck();
        QuackAble gooseAdapter = new QuackCounter(new GooseAdapter(new Goose()));

        Flock flock = new Flock();
        flock.add(duckCall);
        flock.add(mallardDuck);
        flock.add(redHeadDuck);
        flock.add(rubberDuck);
        flock.add(gooseAdapter);

        testQuackAble(flock);

        System.out.println("鸭子叫了" + QuackCounter.getQuackCounter() + "次");
    }

    @Test
    public void testAbstractFactory() {
        AbstractDuckFactory duckFactory = new CountingDuckFactory();
        QuackAble duckCall = duckFactory.createDuckCall();
        QuackAble mallardDuck = duckFactory.createMallardDuck();
        QuackAble redHeadDuck = duckFactory.createRedHeadDuck();
        QuackAble rubberDuck = duckFactory.createRubberDuck();
        QuackAble gooseAdapter = new QuackCounter(new GooseAdapter(new Goose()));

        testQuackAble(duckCall);
        testQuackAble(mallardDuck);
        testQuackAble(redHeadDuck);
        testQuackAble(rubberDuck);
        testQuackAble(gooseAdapter);

        System.out.println("鸭子叫了" + QuackCounter.getQuackCounter() + "次");
    }

    @Test
    public void testDecoration() {
        QuackAble duckCall = new QuackCounter(new DuckCall());
        QuackAble mallardDuck = new QuackCounter(new MallardDuck());
        QuackAble redHeadDuck = new QuackCounter(new RedHeadDuck());
        QuackAble rubberDuck = new QuackCounter(new RubberDuck());
        QuackAble gooseAdapter = new QuackCounter(new GooseAdapter(new Goose()));

        testQuackAble(duckCall);
        testQuackAble(mallardDuck);
        testQuackAble(redHeadDuck);
        testQuackAble(rubberDuck);
        testQuackAble(gooseAdapter);

        System.out.println("鸭子叫了" + QuackCounter.getQuackCounter() + "次");
    }

    @Test
    public void testAdapter() {
        QuackAble duckCall = new DuckCall();
        QuackAble mallardDuck = new MallardDuck();
        QuackAble redHeadDuck = new RedHeadDuck();
        QuackAble rubberDuck = new RubberDuck();
        Goose goose = new Goose();
        GooseAdapter gooseAdapter = new GooseAdapter(goose);

        testQuackAble(duckCall);
        testQuackAble(mallardDuck);
        testQuackAble(redHeadDuck);
        testQuackAble(rubberDuck);
        testQuackAble(gooseAdapter);
    }

    private void testQuackAble(QuackAble quackAble) {
        quackAble.quack();
    }
}
